﻿using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using vroom.wcf.common;
using vroom.wcf.service.cache;
using vroom.wcf.service.cache.Updaters;
using vroom.wcf.service.common;
using vroom.wcf.service.config;


namespace service.test
{

	[TestClass]
	public class ServiceTest
	{

		[TestMethod]
		public void MiscTest()
		{
			int number = -0;
			string numAsString = number.ToString("+#;-#;0");
			System.Diagnostics.Debug.WriteLine("Number: " + numAsString);


			List<string>  _tables = "Position,Bucket".Split(Consts.COMMA).ToList();
			List<DateTime> _dates = new List<DateTime>() { DateTime.Today, DateTime.Today.AddDays(-1), DateTime.Today.AddDays(-2) };

			IEnumerable<Tuple<string, DateTime>> _itemsToCache = _tables.SelectMany((l) => _dates, (l, r) => new Tuple<string, DateTime>(l, r));
		}

		[TestMethod]
		public void IntesectionTest()
		{
			IEnumerable<ITableState> cachedTables = new List<TableState>() 
													{ 
														new TableState() { TableName = Consts.Position + Consts.HYPHEN + DateTime.Today.AddDays(0).ToIso() , TableDate = DateTime.Today.AddDays(0),  TableUpdated = DateTime.UtcNow },
														new TableState() { TableName = Consts.Position + Consts.HYPHEN + DateTime.Today.AddDays(-1).ToIso(), TableDate = DateTime.Today.AddDays(-1), TableUpdated = DateTime.UtcNow },
														new TableState() { TableName = Consts.Position + Consts.HYPHEN + DateTime.Today.AddDays(-2).ToIso(), TableDate = DateTime.Today.AddDays(-2), TableUpdated = DateTime.UtcNow },
													};

			Thread.Sleep(1000 * 5);

			IEnumerable<ITableState> databaseTables = new List<TableState>() 
													{ 
														new TableState() { TableName = Consts.Position + Consts.HYPHEN + DateTime.Today.AddDays(0).ToIso() , TableDate = DateTime.Today.AddDays(0),  TableUpdated = DateTime.UtcNow },														
														new TableState() { TableName = Consts.Position + Consts.HYPHEN + DateTime.Today.AddDays(-2).ToIso(), TableDate = DateTime.Today.AddDays(-2), TableUpdated = DateTime.UtcNow },
													};

			IEnumerable<ITableState> stales = cachedTables.Intersect(databaseTables, new StaleTableComparer()).ToList();

			Assert.AreEqual(stales.Count(), 2);
		}

		[TestMethod]
		public void UnityTest()
		{
			IUnityContainer container = new UnityContainer();
			try
			{
				container.RegisterType<ICacheUpdater, PosCacheUpdater>   (Consts.Position, new PerResolveLifetimeManager(), new InjectionConstructor(typeof(string), typeof(string), typeof(DateTime)));
				container.RegisterType<ICacheUpdater, BucketCacheUpdater>(Consts.Bucket,   new PerResolveLifetimeManager(), new InjectionConstructor(typeof(string), typeof(string), typeof(DateTime)));

				ICacheUpdater cacheUpdater = container.Resolve<ICacheUpdater>(Consts.Position,
																				new ParameterOverride(ParamNames.cacheKey, Consts.Position + Consts.Position + DateTime.Today.ToIso()),
																				new ParameterOverride(ParamNames.tableName, Consts.Position),
																				new ParameterOverride(ParamNames.effectiveDate, DateTime.Today));



			}
			catch (Exception e)
			{
				throw e;
			}

		}


		[TestMethod]
		public void CacheConfigSectionTest()
		{
			CacheConfigSection cacheConfig = (CacheConfigSection)System.Configuration.ConfigurationManager.GetSection("cacheSettings");

			//test Type Correctness
			Assert.IsInstanceOfType(cacheConfig.CacheOnStart, typeof(bool));
			Assert.IsInstanceOfType(cacheConfig.TablesToCache, typeof(string));
			Assert.IsInstanceOfType(cacheConfig.DaysToLoadPriorToToday, typeof(int));
			Assert.IsInstanceOfType(cacheConfig.EvictFromCacheDurationInHours, typeof(double));
			Assert.IsInstanceOfType(cacheConfig.MaxWaitForObjectToPopulateDurationInSeconds, typeof(int));
		}

	}
}
